package cn.fbs.controller.core;

import cn.fbs.enums.MediaEnum;
import cn.fbs.service.ImageDataService;
import cn.fbs.service.TripartiteService;
import cn.fbs.util.Result;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping()
@Api(tags = "三方接口")
public class ShareController {

    @Autowired
    private ImageDataService imageDataService;
    @Autowired private TripartiteService service;


    @ApiOperation("北京-分享目标下的多个文件")
    @PostMapping("imageData/shareData")
    public Object shareData(@RequestBody String str) throws IOException {
        JSONObject jsonObject = JSONObject.parseObject(str);
        List<Long> list = (List<Long>) jsonObject.get("list");
        Integer dataType = (Integer) jsonObject.get("dataType");
        if (dataType == null) {
            return "500,请选择文件类型";
        }
        if (dataType.equals(MediaEnum.IMAGE.getIndex())) {
            return imageDataService.shareImage(list);
        }
        if (dataType.equals(MediaEnum.VIDEO.getIndex())) {
            return imageDataService.shareVideo(list);
        }
        return "500,无匹配的数据类型";
    }





    @ApiOperation("查询所有的媒体类型和目标名称")
    @GetMapping("tripartite/getAllMTLX")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId",value = "图像视频ID",required = true),
            @ApiImplicitParam(name = "flag",value = "标识（1.视频 0.图像）",required = false)
    })
    public Result getAllMTLX(Long dataId, String flag){
        return service.getAllMTLX(dataId, flag);
    }


    @ApiOperation("同步数据")
    @GetMapping("tripartite/synData")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId",value = "图像视频ID",required = true),
            @ApiImplicitParam(name = "mtlxId",value = "媒体类型ID",required = false),
            @ApiImplicitParam(name = "flag",value = "标识（1.视频 0.图像）",required = false)
    })
    public Result synchronizeData(Long dataId,String mtlxId, String flag) throws IOException {
        Result result = new Result();

        if (mtlxId==null||"".equalsIgnoreCase(mtlxId)){
            List<Long> list = new ArrayList<>();
            list.add(dataId);
            Object object=null;
            if ("0".equalsIgnoreCase(flag)){
                object = imageDataService.shareImage(list);
            }
            if ("1".equalsIgnoreCase(flag)){
                object = imageDataService.shareVideo(list);
            }
            result.setCode(200);
            if (object!=null){
                result.setMsg(object.toString());
            }
            return result;
        }
        try {
            return service.synchronizeData(dataId,mtlxId, flag);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("新建目标不能进行文件共享");
            return result;
        }
    }
}
